package com.intel.surface;

import com.intel.bluetooth.ConnectService;
import lombok.Data;
import lombok.SneakyThrows;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

@Data
public class ConnectedDevicePn extends JPanel {

    private static final long serialVersionUID = 1L;
    private JLabel connect;
    private ConnectService connectService;

    @SneakyThrows
    public ConnectedDevicePn(ConnectService connectService) {
        this.connectService = connectService;
        this.setPreferredSize(new Dimension(0, 50));
        this.setLayout(new BorderLayout(0, 0));
        this.setBackground(new Color(255, 255, 255));

        JPanel left = new JPanel(new VFlowLayout());
        left.setBackground(new Color(255, 255, 255));
        {
            JLabel name = new JLabel(connectService.getDeviceConnectService().getRemoteDevice().getFriendlyName(false));
            name.setFont(new Font("黑体", Font.BOLD, 15));
            JLabel address = new JLabel(connectService.getDeviceConnectService().getRemoteDevice().getBluetoothAddress());
            address.setForeground(Color.GRAY);
            address.setFont(new Font("黑体", Font.PLAIN, 13));
            left.add(name);
            left.add(address);
        }
        this.add(left, BorderLayout.CENTER);

        JPanel right = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 12));
        right.setBackground(new Color(255, 255, 255));
        {
            connect = new JLabel();
            connect.setIcon(new ImageIcon("img/connected.png"));
            connect.setToolTipText("断开");
            connect.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));

            right.add(connect);
        }
        this.add(right, BorderLayout.EAST);

        addListeners();
    }

    private void addListeners() {
        connect.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                connectService.closeConnection(false);
                connectService.getUnconnectDevicePn().getFrame().removeLostConnection(ConnectedDevicePn.this);
            }
        });
    }
}
